package com.jone.phone.detail.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jone.phone.detail.R;
import com.jone.phone.detail.model.TeleDetail;
import com.jone.phone.detail.util.DateUtil;

public class DetailView extends Activity {
		
	private ListView listView;
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        setContentView(R.layout.alist);
	        
	        ImageView img1 = (ImageView)this.findViewById(R.id.tab1);
	        ImageView img2 = (ImageView)this.findViewById(R.id.tab2);
	        ImageView img3 = (ImageView)this.findViewById(R.id.tab3);
	        img1.setOnClickListener(header);
	        img2.setOnClickListener(header);
	        img3.setOnClickListener(header);	        
	        
	        
	        listView = (ListView)this.findViewById(R.id.detailList);
	        
	        Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.DAY_OF_MONTH, 1);
	        String selection = TeleDetail.TIME+">="+DateUtil.formatDate(calendar.getTime(), "yyyyMMdd")+"000000";
	        String sortOrder = "_id desc";
	        Cursor c = this.getContentResolver().query(TeleDetail.URI_TELE_DETAIL, null, selection, null, sortOrder);
	        List<TeleDetail> detailList = new ArrayList<TeleDetail>();
	        if(c.moveToFirst()){
	        	while(!c.isAfterLast()){
	        		int type = c.getInt(c.getColumnIndex(TeleDetail.TYPE));
	        		int duration = c.getInt(c.getColumnIndex(TeleDetail.DURATION));	        		
	        		String time = c.getString(c.getColumnIndex(TeleDetail.TIME));
	        		String personName = c.getString(c.getColumnIndex(TeleDetail.PERSON_NAME));
	        		if(personName==null || "".equals(personName)){
	        			personName = c.getString(c.getColumnIndex(TeleDetail.PERSON_NUMBER));
	        		}
	        		TeleDetail detail = new TeleDetail(type,duration,personName,time);
	        		detailList.add(detail);
	        		c.moveToNext();
	        	}
	        }
	        this.startManagingCursor(c);
	        
	        listView.setAdapter(new DetailAdapter(this,detailList));       
	        
	 }
	 
	 private OnClickListener header = new View.OnClickListener() {
 		
 		@Override
 		public void onClick(View v) {
 			switch(v.getId()){			
 			case R.id.tab1:
 				Intent i = new Intent(DetailView.this,TodayView.class);
 				DetailView.this.startActivity(i);
 				break;
 			case R.id.tab2:
 				i = new Intent(DetailView.this,StatView.class);
 				DetailView.this.startActivity(i);
 				break; 			
 			}
 			
 		}
 	};
	 
	 private static class DetailAdapter extends BaseAdapter {
	        private LayoutInflater mInflater;
	        private Bitmap mIcon1;
	        private Bitmap mIcon2;
	        
	        List<TeleDetail> detailList = new ArrayList<TeleDetail>();

	        public DetailAdapter(Context context,List<TeleDetail> detailList) {
	            // Cache the LayoutInflate to avoid asking for a new one each time.
	            mInflater = LayoutInflater.from(context);
	            this.detailList = detailList;

//	            // Icons bound to the rows.
//	            mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
//	            mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
	        }

	        /**
	         * The number of items in the list is determined by the number of speeches
	         * in our array.
	         *
	         * @see android.widget.ListAdapter#getCount()
	         */
	        public int getCount() {
	            return detailList.size();
	        }

	        /**
	         * Since the data comes from an array, just returning the index is
	         * sufficent to get at the data. If we were using a more complex data
	         * structure, we would return whatever object represents one row in the
	         * list.
	         *
	         * @see android.widget.ListAdapter#getItem(int)
	         */
	        public Object getItem(int position) {
	            return detailList.get(position);
	        }

	        /**
	         * Use the array index as a unique id.
	         *
	         * @see android.widget.ListAdapter#getItemId(int)
	         */
	        public long getItemId(int position) {
	            return position;
	        }

	        /**
	         * Make a view to hold each row.
	         *
	         * @see android.widget.ListAdapter#getView(int, android.view.View,
	         *      android.view.ViewGroup)
	         */
	        public View getView(int position, View convertView, ViewGroup parent) {
	            // A ViewHolder keeps references to children views to avoid unneccessary calls
	            // to findViewById() on each row.
	           TeleDetail detail = (TeleDetail)getItem(position); 

	            // When convertView is not null, we can reuse it directly, there is no need
	            // to reinflate it. We only inflate a new View when the convertView supplied
	            // by ListView is null.
	            if (convertView == null) {
	                convertView = mInflater.inflate(R.layout.list_item, null);

	                // Creates a ViewHolder and store references to the two children views
	                // we want to bind data to.
	                
//	                holder.text = (TextView) convertView.findViewById(R.id.text);
//	                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

	            } else {
	                // Get the ViewHolder back to get fast access to the TextView
	                // and the ImageView.
	                //holder = (ViewHolder) convertView.getTag();
	            }

	            // Bind the data efficiently with the holder.
//	            holder.text.setText(DATA[position]);
//	            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
	            
	            TextView txtPersonName = (TextView) convertView.findViewById(R.id.personName);
	            TextView txtDuration = (TextView) convertView.findViewById(R.id.duration);
	            TextView txtFlag = (TextView) convertView.findViewById(R.id.call_flag);	            
	            TextView txtTime = (TextView) convertView.findViewById(R.id.call_time);
	            
	            txtPersonName.setText(detail.getPersonName());
	            txtDuration.setText(DateUtil.getDesc(detail.getDuration()));
	            txtTime.setText(DateUtil.formatDate(detail.getTime(),"yyyy-MM-dd HH:mm:ss"));
	            if(detail.getType() == CallLog.Calls.INCOMING_TYPE){
	            	txtFlag.setTextColor(Color.WHITE);
	            	txtFlag.setText("接电话");
	            	
	            }else{
	            	txtFlag.setTextColor(Color.GREEN);
	            	txtFlag.setText("打电话");
	            }

	            return convertView;
	        }	       
	    }
	 
	 
	 public void onStop(){
	    	this.finish();
	    	super.onDestroy();
	 }

}
